<?php

namespace App\Http\Controllers\Rent;

use App\Http\Controllers\ShopController;
use App\Models\Category as CategoryModel;

class CategoryController extends ShopController
{
    public function hot()
    {
        /* 
            field => 要查询的字段（TP）
            select => 要查询的字段（laravel）
        */
        $HotList = CategoryModel::where('flag','LIKE','%hot%')->where('status','normal')->limit(6)->OrderBy('createtime','desc')->select('id','createtime')->get();

        return $this->success('查询成功',$HotList);
    }

    public function index()
    {
        $page = request('page',1);
        $limit = request('limit',10);

        // 获取每次请求的分页偏移量
        $start = ($page - 1) * $limit;

        // 获取数据的总数
        $count = CategoryModel::where('status','normal')->count();

        // 获取数据
        $list = CategoryModel::where('status','normal')->offset($start)->limit($limit)->OrderBy('createtime','desc')->get();

        $data = [
            'count' => $count,
            'list' => $list
        ];

        if($count > 0)
        {
            return $this->success('查询成功',$data);
        }else{
            return $this->error('暂无数据');
        }
    }

    public function info()
    {
        $id = request('id',0);

        $category = CategoryModel::find($id);

        if(!$category)
        {
            return $this->error('该文章不存在');
        }

        // 上一篇
        $perv = CategoryModel::where('id','<',$id)->OrderBy('id','desc')->first();
        
        // 下一篇
        $next = CategoryModel::where('id','>',$id)->OrderBy('id','asc')->first();

        $data = [
            'perv' => $perv,
            'info' => $category,
            'next' => $next
        ];

        return $this->success('查询成功',$data);
    }
}
